﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Haplous/Size.cs
//  *
//  ***************************************************************************

#endregion

using System.Diagnostics;

using Haplous.Core;

using JetBrains.Annotations;

namespace Haplous
{
    /// <summary>
    ///     A class representing the 2D dimensions.
    /// </summary>
    /// <typeparam name="T">The type in which each dimension is stored.</typeparam>
    [DebuggerDisplay("Size: {Width},{Height}")]
    public class Size<T> : NotifyObject where T : struct
    {
        /// <summary>
        ///     The height, measured in pixels.
        /// </summary>
        private T _height;

        /// <summary>
        ///     Indicates that this object cannot be changed externally.
        /// </summary>
        private bool _isLocked;

        /// <summary>
        ///     The width, measured in pixels.
        /// </summary>
        private T _width;

        /// <summary>
        ///     Initializes a new instance of the <see cref="T:System.Object" /> class.
        /// </summary>
        public Size()
        {}

        /// <summary>
        ///     Initializes a new instance of the <see cref="T:System.Object" /> class.
        /// </summary>
        /// <param name="width">The width, measured in pixels.</param>
        /// <param name="height">The height, measured in pixels.</param>
        public Size(T width, T height)
        {
            _height = height;
            _width = width;
        }

        /// <summary>
        ///     Indicates that this object cannot be changed externally.
        /// </summary>
        /// <remarks>Whilst locked this property can only be changed by the <see cref="Director" /> of an <see cref="Actor" />.</remarks>
        public bool IsLocked
        {
            get { return _isLocked; }
            internal set { _isLocked = value; }
        }

        /// <summary>
        ///     The width, measured in pixels.
        /// </summary>
        public T Width
        {
            get { return _width; }
            set
            {
                if(_isLocked)
                    throw new LockedException("This object is locked.  Changes may only be made by the controller.");

                OnPropertyChanged("Width", ref _width, value);
            }
        }

        /// <summary>
        ///     The height, measured in pixels.
        /// </summary>
        public T Height
        {
            get { return _height; }
            set
            {
                if(_isLocked)
                    throw new LockedException("This object is locked.  Changes may only be made by the controller.");

                OnPropertyChanged("Height", ref _height, value);
            }
        }

        /// <summary>
        ///     Sets the specified width and height.
        /// </summary>
        /// <param name="width">The width, measured in pixels.</param>
        /// <param name="height">The height, measured in pixels.</param>
        public void Set(T width, T height)
        {
            if(_isLocked)
                throw new LockedException("This object is locked.  Changes may only be made by the controller.");

            var widthChanged = !Equals(width, _width);
            if(widthChanged)
                _width = width;

            var heightChanged = !Equals(height, _height);
            if(heightChanged)
                _height = height;

            if(widthChanged)
                OnPropertyChanged("Width");
            if(heightChanged)
                OnPropertyChanged("Height");
        }

        /// <summary>
        ///     Sets the width and height from the specified <see cref="T:Size" /> object.
        /// </summary>
        /// <param name="newSize">
        ///     The <see cref="T:Size" /> object from which to take the new <see cref="Width" /> and
        ///     <see cref="Height" /> values.
        /// </param>
        internal void Set([NotNull] Size<T> newSize)
        {
            var width = newSize.Width;
            var height = newSize.Height;

            var widthChanged = !Equals(width, _width);
            if(widthChanged)
                _width = width;

            var heightChanged = !Equals(height, _height);
            if(heightChanged)
                _height = height;

            if(widthChanged)
                OnPropertyChanged("Width");
            if(heightChanged)
                OnPropertyChanged("Height");
        }
    }
}